//
//  SSDKRealTimeTrace.m
//  SuperSDK
//
//  Created by 涂俊 on 2018/3/27.
//  Copyright © 2018年 WYHT. All rights reserved.
//

#import "SSDKRealTimeTrace.h"
#import "SSDKEventKeys.h"
#import "SSDKEventManager.h"
#import "SSDKDataKeys.h"

#import "SuperSDKInst.h"
#import "SSDKUserManager.h"
#import "SSDKUserInfo.h"
#import "SSDKSwitchUtil.h"

#import "SuperSDKPluginTools/SSDKPluginDataHelper.h"
#import "SuperSDKPluginTools/SSDKPluginHttpUtil.h"
#import "SuperSDKPluginTools/SSDKPluginJsonUtil.h"
#import "SuperSDKPluginTools/SSDKPluginMD5.h"


@implementation SSDKRealTimeTrace

static NSString *key = @"sdu239%ndSDF&&8337";

-(void) initInfo {
    [SSDKEventManager addObsver:self selector:@selector(submitData:) name:EVENT_SUBMITDATA object:nil className:@"SSDKRealTimeTrace"];
}

-(void) submitData:(NSNotification*) notification {
    NSString *ext = [[notification object] objectForKey:SUBMIT_EXT];
    [SSDKSwitchUtil allValueWithDelegate:^(NSDictionary *value) {
        // 只有服务器开关开启，才开启上报数据，默认是关闭
        if (value == nil || ![@"true" isEqualToString:[value objectForKey:@"submit_realtime_switch"]]) {
            NSLog(@"实时上报功能关闭！");
            return;
        }
        NSString *url = [value objectForKey:@"submit_realtime_url"];
        if (!url) {
            NSLog(@"实时上报url为空，不上报！");
            return;
        }
        if([SUBMIT_CREATE_ROLE_EVENT isEqualToString:ext]) {//创建角色
            NSLog(@"%@,实时上报数据", ext);
            NSMutableDictionary *params = [notification object];
            NSMutableDictionary *wrapParams = [[NSMutableDictionary alloc] init];
            NSString *appid = [[SuperSDKInst getInstance] appId];
            [wrapParams setValue:appid forKey:@"appid"];//supersdk的appid
            [wrapParams setValue:[[SuperSDKInst getInstance] channelId] forKey:@"channel"];//渠道id
            [wrapParams setValue:[NSString stringWithFormat:@"%d", [[SuperSDKInst getInstance] serverID]] forKey:@"zone"];//区服
            [wrapParams setValue:[params objectForKey:@"roleId"] forKey:@"playerid"];//角色id
            [wrapParams setValue:[[[SuperSDKInst getInstance] clientInfo] valueForKey:SSDK_L_DEVICE_ID] forKey:@"deviceid"];//设备id
            long long clientTime = [[NSDate date] timeIntervalSince1970]*1000;
            NSString *cTimeStr = [NSString stringWithFormat:@"%lld", clientTime];
            [wrapParams setValue:cTimeStr forKey:@"ctime"];//客户端上报时间(毫秒)
            NSString *roleTime = [params objectForKey:@"role_create_time"];
            long long happenTime;
            if (roleTime == nil || roleTime.length == 0) {
                happenTime = clientTime;
            } else if (roleTime.length == 10) {
                happenTime = [roleTime intValue] * 1000;
            } else if (roleTime.length == 13) {
                happenTime = [roleTime longLongValue];
            } else {
                happenTime = clientTime;
            }
            [wrapParams setValue:[NSString stringWithFormat:@"%lld", happenTime] forKey:@"time"];//事件发生时间(毫秒)
            // 签名：ctime+appid+key计算md5小写
            [wrapParams setValue:[SSDKPluginMD5 md5:[[cTimeStr stringByAppendingString:appid] stringByAppendingString:key]] forKey:@"sign"];
            [SSDKPluginHttpUtil postJsonWithUrl:url
                                           data:wrapParams
                                       delegate:^(BOOL networkAvaliable, NSInteger status, NSString *resData) {
                                           if (!networkAvaliable) {
                                               NSLog(@"实时上报网络不可用!url=%@", url);
                                               return;
                                           }
                                           NSLog(@"实时上报status=%ld,返回=%@", (long)status, resData);
                                       }];
        }
    }];
}

@end
